<?php

namespace App\Http\Controllers\Teacher;

use App\Criteria\System\User\UserTeacherQueryCriteria;
use App\Criteria\Teacher\Teacher\TeacherBaseQueryCriteria;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CurlRecordMethod;
use App\Http\Requests\teacher\TeacherBaseRequest;
use App\Libs\Tools\UploadTools;
use App\Repositories\System\UserRepository;
use App\Repositories\Teacher\TeacherRepository;
use App\Validators\Teacher\TeacherValidator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
use Prettus\Validator\Contracts\ValidatorInterface;


class TeachersController extends Controller{

    use CurlRecordMethod;

    protected $index_page='teacher.teacher.teacherMain';
    protected $add_page='teacher.teacher.teacherAdd';
    protected $edit_page='teacher.teacher.teacherEdit';

    protected $repository;
    protected $validator;
    protected $request;
    protected $userRepository;

    public function __construct(TeacherRepository $repository,
                                  TeacherValidator $validator,
                                  TeacherBaseRequest $teacherBaseRequest,
                                  UserRepository $userRepository){
        $this->repository = $repository;
        $this->validator  = $validator;
        $this->request    = $teacherBaseRequest;
        $this->userRepository =$userRepository;
    }

    public function getBaseQueryCriteria(){
        return new TeacherBaseQueryCriteria;
    }

    protected function get_edit_data(){
        $id=$this->request->get('id');
        $editEntity = $this->userRepository->skipPresenter()->pushCriteria(new UserTeacherQueryCriteria)->find($id);
        return view($this->edit_page,compact('editEntity'));
    }

    public function info_edit(){
        $id=Input::get('id');
        $teacher_count=$this->repository->skipPresenter()->findByField('id',$id)->count();
        try {
            $all=$this->request->all();
            if($teacher_count==0) { //新建
                $all=$this->wrap_user_info($all,true);
                $this->validator->with($all)->passesOrFail(ValidatorInterface::RULE_CREATE);
                $this->repository->create($all);
            }else{
                $all=$this->wrap_user_info($all,false);
                $this->repository->update($all, $id);
            }

            return $this->return_json_success();
        } catch (ValidatorException $e) {
            return $this->return_json_error($e);
        }

    }

    public function photo_edit(){
        $id=$this->request->get('id');
        $teacher_count=$this->repository->skipPresenter()->findByField('id',$id)->count();
        if($teacher_count==0){
            return $this->return_json_error_message('系统还没有初始化该教师信息，请先填写基本资料');
        }
        $upload_file=$this->request->file('photo');
        $relativePath=UploadTools::generateUploadFileRelativePath($upload_file,'avatars/'.$id);
        $model=$this->repository->skipPresenter()->find($id);
        $model->updated_user=Auth::user()['name'];
        $model->photo=$relativePath;
        $model->save();
        Storage::put($relativePath, file_get_contents($upload_file->getRealPath()));
        $url = UploadTools::getFileUrl($relativePath);
        $msg=['url'=>$url];
        return $this->return_json_success($msg);

    }

    public function honour(){
        return view('teacher.honour.honourMain');
    }
    public function honour_edit(){
        $teacher_id=Input::get('teacher_id');
        return view('teacher.honour.honourEdit',compact('teacher_id'));
    }
}
